package common.system.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
import common.util.StrutsUtil;
/**
 * 图片上出的公共类
 * @author 奥斯卡
 *
 */
public class AdminAction extends ActionSupport {
	private static final long serialVersionUID = 1L;
	private String resultStr;
	private String resultCode;
	private File Filedata;
	private String Filename;
	private String file;
	private String curPageTag;
	
	
	private String myContent;
	/**
	 * 登录界面
	 * @return
	 */
	public String login(){
		if(StrutsUtil.validateLogin(ServletActionContext.getRequest().getSession())){
			return SUCCESS;
		}
		return "notlogin";
	}
	public String commonFunc(){
		return SUCCESS;
	}
	public String admin(){
		return SUCCESS;
	}
	public String index(){
		return SUCCESS;
	}
	public String test(){
		System.out.println("neirong==="+myContent);
		return SUCCESS;
	}
	public String uploadFile(){
		InputStream is =null;
		OutputStream os=null;
		try{
			if(Filename==null || "".equals(Filename.trim())){
				resultCode="0";
				resultStr="上传文件失败!错误码:传入的文件名为空!";
				return SUCCESS;
			}
			String basePath = ServletActionContext.getServletContext().getRealPath("/");
			long prifix = System.currentTimeMillis();
			String date = new SimpleDateFormat("yyyyMMdd").format(new Date(prifix));
			
			String suffix = Filename.substring(Filename.indexOf(".")+1, Filename.length());
			
			String uploadPath  = basePath + "upload/" + date;
			File baseFile = new File(uploadPath);
			if(!baseFile.exists() || !baseFile.isDirectory()){
				if(!baseFile.mkdirs()){
					resultCode="0";
					resultStr="上传文件失败!错误码:创建文件夹失败!";
					return SUCCESS;
				}
			}
			File uploadFile =new File(uploadPath+"/"+prifix+"."+suffix);
			if(!uploadFile.createNewFile()){
				resultCode="0";
				resultStr="上传文件失败!错误码:创建上传文件失败!";
				return SUCCESS;
			}
			is= new FileInputStream(Filedata);
			os = new FileOutputStream(uploadFile);
			int length = 0;
			byte[] bytes = new byte[10240];
			while((length=is.read(bytes))>0){
				os.write(bytes, 0, length);
			}
			resultCode="1";
			resultStr="上传文件成功";
			file="/upload/" + date+"/"+prifix+"."+suffix;
			return SUCCESS;
		}catch(Exception e){
			e.printStackTrace();
			resultCode="0";
			resultStr="上传文件发生异常";
		}finally{
			if(os!=null){
				try {
					os.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if(is!=null){
				try {
					is.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return SUCCESS;
	}
	//========= getter and setter ======== 方法
	
	public String getResultStr() {
		return resultStr;
	}
	public String getCurPageTag() {
		return curPageTag;
	}
	public void setCurPageTag(String curPageTag) {
		this.curPageTag = curPageTag;
	}
	public String getFile() {
		return file;
	}
	public void setFile(String file) {
		this.file = file;
	}
	public File getFiledata() {
		return Filedata;
	}
	public void setFiledata(File filedata) {
		Filedata = filedata;
	}
	public String getFilename() {
		return Filename;
	}
	public void setFilename(String filename) {
		Filename = filename;
	}
	public void setResultStr(String resultStr) {
		this.resultStr = resultStr;
	}
	public String getResultCode() {
		return resultCode;
	}
	public void setResultCode(String resultCode) {
		this.resultCode = resultCode;
	}
	public String getMyContent() {
		return myContent;
	}
	public void setMyContent(String myContent) {
		this.myContent = myContent;
	}
	
}
